$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'production/erporderproduction/sellerlist',
        datatype: "json",
        colModel: [
            {
                label: '操作', width: 50, align: 'center',
                formatter: function (value, options, row) {
                    // 生产单生产状态:1.待生产,2.生产中,3.生产完成,4.取消,5.待收货,6.已收货
                    var productionStatus = row.productionStatus;
                    // 分配订单收货状态 0:未收货，1:已收货
                    var allotReceivedStatus = row.allotReceivedStatus == null ? 0 : row.allotReceivedStatus;
                    if (productionStatus == 3 && allotReceivedStatus == 0){
                        return '<a href="javascript:;" onclick="return confirmReceive(\'' + options.rowId + '\');">验货确定</a>';
                    }
                    return '--';
                }
            },
            {label: 'productionId', name: 'productionId', index: 'production_id', key: true,hidden:true, align: 'center'},
            {label: '原订单号', name: 'orderNo', index: 'order_no', width: 80, align: 'center',
                formatter: function (value) {
                    return '<a href="javascript:;" onclick="return vm.getInfo(\'' + value + '\');">'+ value +'</a>'
                }
            },
            {label: '金额', name: 'payableAmount', index: 'payable_amount', width: 60, align: 'center'},
            {label: '产品名称', name: 'erpItemName', index: '', width: 60, align: 'center'},
            {label: '产品规格', name: 'addon', index: 'addon', width: 130, align: 'center'},
            {label: '分配时间', name: 'createTime', index: 'create_time', width: 100, align: 'center'},
            {label: '工序', name: 'attributeName', index: 'attribute_name', width: 50, align: 'center'},
            {label: '数量', name: 'nums', index: 'nums', width: 40, align: 'center'},
            {label: '单位', name: 'unit', index: 'unit', width: 40, align: 'center'},
            {
                label: '生产状态',
                name: 'productionStatus',
                index: 'production_status',
                width: 50,
                align: 'center',
                hidden: true,
                align: 'center'
            },
            {
                label: '生产状态', name: '', index: 'production_status', width: 50, align: 'center',
                formatter: function (value, options, row) {
                    var valueText = "";
                    var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                    $.each(userData.orderProductionStatusEnum, function (index, module) {
                        if (module.id == row.productionStatus) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
            },
            {label: '验货状态', name: 'allotReceivedStatus', width: 80, align: 'center',
                formatter: function (value) {
                    if (value == null){
                        value = 0;
                    }
                    return value == 0 ? '待验货' : '已验货';
                }
            },
            {label: '提现方式', name: 'onlineCash', width: 80, align: 'center',
                formatter: function (value) {
                    return value == 0 ? '线下' : '线上';
                }
            },
            {label: '生产商', name: 'producerName', index: 'producerName', width: 120, align: 'center'},
            {label: '图片', name: 'sendPicsUrl', index: '', width: 35, align: 'center',
                formatter: function (value, options, row) {
                    if (isBlank(value)){
                        return '';
                    }
                    var urlArr = value.split(',');
                    var html = '<img class="img-thumbnail" src="'+window.parent.vm.ossHost +urlArr[0]+'">';
                    return html;
                }
            },
            {label: '跟单员', name: 'pmcName', width: 50, align: 'center'},
            {label: '第三方单号', name: 'productionOrder', width: 100, align: 'center'},
            {label: '总额', name: 'allotTotalAmount', index: '', width: 50, align: 'center'},
            {label: '调价', name: 'allotDiscount', index: '', width: 50, align: 'center'},
            {label: '应付金额', name: 'allotPayableAmount', index: '', width: 50, align: 'center'},
        ],
        viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList: [25, 50, 100],
        rownumbers: true,
        rownumWidth: 40,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        postData: {
            allotReceivedStatus: vm.q.allotReceivedStatus
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            // 隐藏grid底部滚动条
            // $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

            // 图片预览初始化
            $('.img-thumbnail').viewer();

            var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
            vm.e.orderProductionStatusEnum = userData.orderProductionStatusEnum;
            vm.e.goodsTypeEnum = userData.goodsTypeEnum;
        }
    });
    // 显示序号
    $("#jqGrid").jqGrid('setLabel',0, '序号', '{width:80}');
});

/**
 * 验货确定
 */
function confirmReceive(rowId) {
    var rowData = $("#jqGrid").jqGrid('getRowData', rowId);
    var orderNo = rowData.orderNo;
    var message = "单号：<span class='text-primary'>"+ orderNo +"</span> 是否验货确定？";
    confirm(message, function(){
        var productionId = rowData.productionId;
        $.ajax({
            type: "GET",
            url: baseURL + "production/erporderproduction/confirmReceive",
            contentType: "application/json",
            data: {
                'productionId': productionId,
            },
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            showOrder: false,
            erpOrderProduction: {},
            erpOrder: {},
            orderInfo: {},
            csIdList: [],
            remarkList: [],
            erpOrderItemEntityList: [],
            erpOrderProductionList: [],
            userPaymentList: [], // 支付信息列表
            pmcIdList:[], // 跟单员列表
            q: {
                orderNo: '',
                productionOrder: '',
                productionStatus: '',
                financeStatus: '',
                producerName: '',
                producerType: '',
                pmcId: '',
                allotReceivedStatus: 0,
            },
            e: {
                orderProductionStatusEnum: [],
                goodsTypeEnum: [],
            },
        },
        methods: {
            query: function () {
                vm.reload();
            },
            /* 查询重置 */
            reset: function () {
                vm.q.orderNo = '';
                vm.q.productionStatus = '';
                vm.q.financeStatus = '';
                vm.q.productionOrder = '';
                vm.q.producerName = '';
                vm.q.producerType = '';
                vm.q.pmcId = '';
                vm.q.allotReceivedStatus = '';
                vm.reload();
            },
            getInfo: function(orderNo){
                $.get(baseURL + "order/erporder/allotInfo/" + orderNo, function(r){
                    vm.orderInfo = r.orderInfo;
                    vm.erpOrder = r.erpOrder;
                    vm.csIdList = r.csIdList;
                    vm.pmcIdList = r.pmcIdList;
                    vm.remarkList = r.remarkList;
                    vm.userPaymentList = r.userPaymentList;
                    vm.erpOrderItemEntityList = r.erpOrderItemEntityList;
                    vm.erpOrderProductionList = r.erpOrderProductionList;
                    vm.showOrder = true;
                    vm.showList = false;
                });
            },
            reload: function (event) {
                vm.showList = true;
                vm.showOrder = false;
                var page = $("#jqGrid").jqGrid('getGridParam','page');
                $("#jqGrid").jqGrid('setGridParam', {
                    page: page,
                    postData: {
                        'orderNo': vm.q.orderNo,
                        'productionStatus': vm.q.productionStatus,
                        'financeStatus': vm.q.financeStatus,
                        'productionOrder': vm.q.productionOrder,
                        'producerName': vm.q.producerName,
                        'producerType': vm.q.producerType,
                        'pmcId': vm.q.pmcId,
                        'allotReceivedStatus': vm.q.allotReceivedStatus,
                    }
                }).trigger("reloadGrid");
            },
            reback: function () {
                vm.showList = true;
                vm.showOrder = false;
            },
        },
    })
;
